package io.net;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class BIODemo {

    public static void main(String[] args) throws IOException, InterruptedException {

        // 异步起动服务端
        new Thread(() -> {
            try {
                new Server(9999).start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();

        // 等待服务端启动完成
        Thread.sleep(3000);

        // 异步启动多个客户端
        for (int i = 0; i < 50; i++) {
            new Thread(() -> {
                try {
                    new Client("127.0.0.1", 9999).run();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }

    static class Server {

        private ServerSocket serverSocket;
        private ExecutorService executorService;

        public Server(int port) throws IOException {
            serverSocket = new ServerSocket(port);
            executorService = Executors.newCachedThreadPool();
        }

        public void start() throws IOException {
            while (true) {
                final Socket socket = serverSocket.accept();
                executorService.execute(() -> {
                    try {
                        System.out.println("服务端收到内容：");
                        BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                        String line;
                        while ((line = reader.readLine()) != null) {
                            System.out.println(line);
                        }
                        socket.shutdownInput();

                        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                        writer.write("我是：" + Thread.currentThread().getName() + "，OVER");
                        writer.flush();
                        socket.shutdownOutput();

                        reader.close();
                        writer.close();
                        socket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                });
            }
        }
    }

    static class Client {

        private Socket socket;

        public Client(String host, int port) throws IOException {
            socket = new Socket(host, port);
        }

        public void run() {
            try {
                BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                writer.write("你好，BIO");
                writer.flush();
                socket.shutdownOutput();

                System.out.println("客户端收到内容：");
                BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String line;
                while ((line = reader.readLine()) != null) {
                    System.out.println(line);
                }
                socket.shutdownInput();

                writer.close();
                reader.close();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
